var gidThis;//当前页面商品ID
var sellerName;//当前页面商品的上传者昵称
var sellerID;//当前页面商品的上传者ID
var replyTo;//提交评论时回复的用户

//计算并显示评论数量，全局变量记录当前gID
function onLoad(gid,name,id){
	initNav();
	var uList=document.getElementById("commentList");
	var cNum=document.getElementById("commentNum");
	cNum.textContent=uList.getElementsByTagName("li").length;
	gidThis=gid;
	sellerName=name;
	sellerID=id;
	replyTo='';
}

//ajax提交评论
function submitComment(){
	var comment=document.getElementById("commentIn").value;
	if(comment == ''){
		alert("评论内容为空");
		return;
	}
	var time=getTimeStr();
	console.log(time);
	var user=getCookie("username");
	if(user==""){
		window.location="/register";
		return;
	}
	$.ajax({
		type: 'POST',
		url: '/add_comment',
		data: {
			'gid': String(gidThis),
			'time': time,
			'commenter': user,
			'text': comment,
			'replyTo': replyTo
		},
		success: function(){
			document.getElementById("commentIn").value='';
			alert("成功");
			location.reload();
		},
		fail: function(){
			alert("失败");
		},
		error: function(){
			alert("服务器内部错误");
		}
	});
}

//购买
function buy(){
	var buyer=getCookie("username");
	if(buyer==""){
		window.location="/login";
	}
	var dealData=new FormData();
	dealData.append("gid",gidThis);
	dealData.append("buyer_id",getCookie("userid"));
	dealData.append("seller_id",sellerID);
	dealData.append("time",getTimeStr());
	$.ajax({
		type: 'post',
		url: '/buy',
		data: dealData,
		processData: false,
		contentType: false,
		success: function(arg){
			alert("已申请购买，服务器返回:"+arg);
		},
		fail: function(){
			alert("请求失败，请稍后再试");
		},
		error: function(){
			alert("服务器内部错误");
		}
	});
}

//收藏
function like(){
	var uid=getCookie("userid");
	if(uid==""){
		window.location="/login";
	}
	var dealData=new FormData();
	dealData.append("gid",gidThis);
	dealData.append("uid",uid);
	$.ajax({
		type: 'post',
		url: '/like',
		data: dealData,
		processData: false,
		contentType: false,
		success: function(arg){
			alert(arg);
		},
		fail: function(){
			alert("请求失败，请稍后再试");
		},
		error: function(){
			alert("服务器内部错误");
		}
	});
}

//删除评论
function removeComment(cid,commenter){
	if(getCookie("userid")==""){
		window.location="/login";
		return;
	}
	// else{
	// 	var name=getCookie("username");
	// 	if(name!=commenter){
	// 		alert("您无权删除别人的评论");
	// 		return;
	// 	}
	// }
	var data=new FormData();
	data.append("cid",cid);
	data.append("uid",getCookie("userid"));
	data.append("commenter",commenter);
	$.ajax({
		type: 'post',
		url: '/remove_cmt',
		data: data,
		processData: false,
		contentType: false,
		success: function(arg){
			alert(arg);
			location.reload();
		},
		fail: function(){
			alert("请求失败，请稍后再试");
		},
		error: function(){
			alert("服务器内部错误");
		}
	});
}

//设置回复的用户
function setReply(to){
	replyTo=to;
	alert("评论回复："+to);
	var cmtInput=document.getElementById("commentIn");
	cmtInput.setAttribute("placeHolder","回复 "+replyTo);
}

//查看卖家主页
function gotoSellerInfo(id){
	var curID=getCookie("userid");
	if(curID==id){
		window.open("/myinfo");
	}else{
		window.open("/user_"+String(id));
	}
}

//删除商品
function removeGoods(gid){
	if(!confirm("即将进行删除操作，请确认此商品违规！")){
		return;
	}
	$.ajax({
		type: 'post',
		url: '/remove_goods',
		data: {
			'gid': gid
		},
		success: function(arg){
			alert(arg);
			window.location="/myinfo";
		},
		fail: function(){
			alert("请求失败");
			window.location="/myinfo";
		},
		error: function(){
			alert("服务器内部错误");
			window.location="/myinfo";
		}
	});
}